package com.mypage.view.activity.beautMore;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.cloudcc.mobile.ExtraConstant;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.ReceiverConstant;
import com.cloudcc.mobile.entity.beau.BeauInfoEntity;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.manager.UrlManager;
import com.cloudcc.mobile.view.web.CloudWebViewActivity;
import com.cloudcc.mobile.view.web.WebSyncDefaultProxy;
import com.mypage.adapter.NchageRecorderAdapter;
import com.mypage.utils.SaveTemporaryData;
import com.mypage.utils.UtilsShowDialog;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ChangeRecordActivity extends Activity {
    private String mEns = RunTimeManager.getInstance().getlanguage();
    public static ChangeRecordActivity instance = null;
    public List<BeauInfoEntity.ObjChange.TypeChange> mList;

    public NchageRecorderAdapter mAdapter;
    private UtilsShowDialog showDialog;
    @Bind(R.id.btnCancel)
    Button btnCancel;
    @Bind(R.id.btnNext)
    Button btnNext;
    @Bind(R.id.listView)
    ListView listView;
    private String mEntityId;
    private String mType;
    private boolean mBtnBool;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_record);
        instance = this;
        ButterKnife.bind(this);
        initData();
        initView();
        initListener();
    }

    @OnClick(R.id.btnCancel)
    public void btnCancel() {
        finish();
    }

    @OnClick(R.id.btnNext)
    public void btnNext() {
        if (mBtnBool) {
            nextStep();
        }
    }

    public void initData() {
        showDialog = new UtilsShowDialog(this);
        if ("en".equals(mEns)) {
            showDialog.onShowLoadingDialog("loading...");
        } else {
            showDialog.onShowLoadingDialog("正在加载...");
        }

        mList = (List<BeauInfoEntity.ObjChange.TypeChange>) getIntent().getSerializableExtra("LIST");
        mEntityId = getIntent().getStringExtra("mEntityId");
        mType = getIntent().getStringExtra("mType");
        mAdapter = new NchageRecorderAdapter(this, mList, mType);
    }

    public void initView() {
        listView.setAdapter(mAdapter);
        showDialog.onDismissLoadingDialog();
    }

    public void initListener() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                NchageRecorderAdapter.ViewHolder h = (NchageRecorderAdapter.ViewHolder) view.getTag();
                cancelCH();//单选开关，关闭就成了多选来
                if (h.checkBox.isChecked()) {
                    NchageRecorderAdapter.getIsSelected().put(position, false);
                    btnNext.setTextColor(Color.rgb(204, 204, 204));
                    mType = ChangeRecordActivity.this.getIntent().getStringExtra("mType");
                    mBtnBool = false;
                } else {
                    NchageRecorderAdapter.getIsSelected().put(position, true);
                    btnNext.setTextColor(Color.rgb(0, 123, 199));
                    mType = mList.get(position).Id;
                    mBtnBool = true;
                }

                mAdapter.notifyDataSetChanged();
            }
        });
        for (int i = 0;i<mList.size();i++){
            if(mType.equals(mList.get(i).Id)){
                mBtnBool = true;
                NchageRecorderAdapter.getIsSelected().put(i, true);
                btnNext.setTextColor(Color.rgb(204, 204, 204));
                mBtnBool = false;
            }
        }
    }

    public void cancelCH() {
        for (int i = 0; i < mList.size(); i++) {
            if (NchageRecorderAdapter.getIsSelected().get(i)) {
                NchageRecorderAdapter.getIsSelected().put(i, false);

            }
        }
    }

    public void nextStep() {
        String ids = mType;
        String mSweb = "selectedRecordType";
        Intent intent = new Intent(this, CloudWebViewActivity.class);
//				RunTimeManager.getInstance().setIsrem(false);
        WebSyncDefaultProxy.MonitorParam monitorParam = new WebSyncDefaultProxy.MonitorParam(new String[]{"m=save",
                "m=edit"}, WebSyncDefaultProxy.STEP_START,
                new String[]{"/WeiXin_index.action"}, true,
                WebSyncDefaultProxy.STEP_OVERRIDEURL);
        intent.putExtra(
                "callback",
                WebSyncDefaultProxy
                        .creatDefaultProxy(ReceiverConstant.RECEIVE_REFRESH)
                        .addMonitorParam(monitorParam).addHomeMonitor());

        if (SaveTemporaryData.isEventAndTask){
            intent.putExtra(
                    ExtraConstant.EXTRA_URL,
                    UrlManager.getManager().addReturnUrl(
                            UrlManager.getManager().getEntityDetailEditUrlTaskEvent(
                                    mEntityId + "&" + mSweb + "=" + ids)));
        }else{
            intent.putExtra(
                    ExtraConstant.EXTRA_URL,
                    UrlManager.getManager().addReturnUrl(
                            UrlManager.getManager().getEntityDetailEditUrl(
                                    mEntityId + "&" + mSweb + "=" + ids)));
        }

        startActivity(intent);
    }
}
